//
//  ECSystemNotiDetialVc.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/25.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECSystemNotiDetialVc.h"
#import <WebKit/WebKit.h>

@interface ECSystemNotiDetialVc ()<WKNavigationDelegate>{
    WKWebView *webView;
    UILabel *titleLab;
    UILabel *timeLab;
}

@end

@implementation ECSystemNotiDetialVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知详情";
    
    [self getDataWithURL];
    
    titleLab = [[UILabel alloc]init];
    [self.view addSubview:titleLab];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont systemFontOfSize:18.f];
    
    timeLab = [[UILabel alloc]init];
    [self.view addSubview:timeLab];
    timeLab.textAlignment = NSTextAlignmentCenter;
    timeLab.font = [UIFont systemFontOfSize:14.f];
    timeLab.textColor = [UIColor grayColor];
    
    
    // 自适应屏幕宽度js
    NSString *jScript =
    @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
    
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
    
    webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:wkWebConfig];
    webView.scrollView.backgroundColor = [UIColor whiteColor];
    webView.navigationDelegate = self;
    [self.view addSubview:webView];
    
    WeakSelf(weakSelf);
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.width.equalTo(weakSelf.view.mas_width);
        make.height.equalTo(@18);
        make.top.equalTo(@30);
    }];
    
    [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.top.equalTo(titleLab.mas_bottom).offset(20);
        make.width.equalTo(titleLab.mas_width);
        make.height.equalTo(@15);
    }];
    
    [webView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLab.mas_bottom).offset(20);
        make.left.equalTo(timeLab.mas_left);
        make.width.equalTo(timeLab.mas_width);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
}

/*  加载数据  */
-(void)getDataWithURL{
    DLog(@"%@",[[UserInfoManager shareUser] token]);
    [HWHttpTool requestWithMethod:@"GET"
                              url:URL_GET_SystemNotiDetail(self.batchId)
                           params:nil
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              DLog(@"%@",json);
                              titleLab.text = json[@"data"][@"title"];
                              timeLab.text = json[@"data"][@"createdTime"];
                              
                              NSString *headerString = @"<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header>";
                              [webView loadHTMLString:[headerString stringByAppendingString:json[@"data"][@"content"]] baseURL:nil];
                          }
                          failure:^(NSError *error) {
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              DLog(@"%@",str);
                              if ([str isEqualToString:@"通知已被管理员删除"]) {
                                  [MBProgressHUD showTipMessageInWindow:str];
                                  [self.navigationController popViewControllerAnimated:YES];
                                  return ;
                              }
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:kAlertTipOfInternetFailure];
                              }
                          }];
}

// WKNavigationDelegate 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
